package com.yidu.InventoryManagement.controller;

import com.yidu.InventoryManagement.entity.Cash;
import com.yidu.InventoryManagement.entity.CashStock;
import com.yidu.InventoryManagement.service.CashStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁俊
 * @date 2022/12/7
 * &@version 1.0
 */
@RestController
public class CashStockController {
    @Autowired
    private CashStockService cashStockService;

    @RequestMapping("/selectCashStock")
    public Map selectCashStock(int page, int limit, String accountName, String date){
        System.out.println("进来了查询"+accountName+"\t"+date+"\t"+page+"\t"+limit);
        //判断accountName==null
        if (accountName==null){
            accountName="";
        }
        //判断data==null
        if (date==null){
            date="";
        }
        //根据当前页，每页显示数，名称，时间 查询加分页selectCashStock
        List<CashStock> cashStocks = cashStockService.selectCashStock(page, limit, '%' + accountName + '%', '%' + date + '%');

        //根据名称时间查行数selectCount
        int count = cashStockService.selectCount('%' + accountName + '%', '%' + date + '%');
        System.out.println("行数"+count);
        //map集合
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",cashStocks);
        return map;
    }


    //多个删除
    @RequestMapping("/deleteCashStock")
    public int deleteCashStock(String cashStockId){
        System.out.println("进来了删除的方法"+cashStockId);
        //根据id删除
        int i = cashStockService.deleteCashStock(cashStockId);
        if(i>0){
            System.out.println("删除成功"+i);
        }else{
            System.out.println("删除失败"+i);
        }
        return i;
    }

    //添加
    @RequestMapping("/insertCashStock")
    public int insertCashStock(CashStock cashStock,String date){
        System.out.println("进来了添加的方法"+date);
        //查询id是否为空selectCashStockIdd
        int s = cashStockService.selectCashStockIdd();
        int i=0;
        if (s==0){
            //现金库存对象FA20221207,fundid
            CashStock  cashStock1=new CashStock("FA20221207","159934",cashStock.getCashId(),date,1,cashStock.getMoney(),cashStock.getRemark());
             //添加语句insertCashStock
            i = cashStockService.insertCashStock(cashStock1);

        }else{
            //获取最大id方法selectDa。s
            String cashStock2 = cashStockService.selectDa().substring(2,10);
            System.out.println("数据库最大id"+cashStock2);
            //id自动增长类
            IdUtil idUtil=new IdUtil();
            //把获取的最大id放到id自动增长类方法中FA拼接
            String id = "FA"+new com.yidu.InventoryManagement.Util.IdUtil().createID(cashStock2);
            //CashStock对象最大id，fundid手
            CashStock  cashStock1=new CashStock(id,"159934",cashStock.getCashId(),date,1,cashStock.getMoney(),cashStock.getRemark());
           //添加语句insertCashStock
            i = cashStockService.insertCashStock(cashStock1);
        }
        if(i>0){
            System.out.println("添加成功"+i);
        }else{
            System.out.println("添加失败"+i);
        }
        return i;
    }


    //修改
    @RequestMapping("/updateCashStock")
    public int updateCashStock(CashStock cashStock){
        System.out.println("进来了修改的方法"+cashStock.getDate()+"备注"+cashStock.getRemark());
        //根据CashStockId查询现金库存数据方法selectCashStockId
        CashStock cashStock2 = cashStockService.selectCashStockId(cashStock.getCashStockId());
        //CashStock对象把cashStock2取到的fundid和InitialState放进去
        CashStock cashStock1=new CashStock(cashStock.getCashStockId(),cashStock2.getFundId(),cashStock.getCashId(),cashStock.getDate(),cashStock2.getInitialState(),cashStock.getMoney(),cashStock.getRemark());
       //修改语句updateCashStock
        int i = cashStockService.updateCashStock(cashStock1);
        if(i>0){
            System.out.println("修改成功"+i);
        }else{
            System.out.println("修改失败"+i);
        }
        return i;
    }


    @RequestMapping("/selectCash")
    public Map selectCash(int page, int limit){
        System.out.println("进来了下拉表格"+page+limit);
        //根据当前页每页显示数查询加分页selectCash
        List<Cash> cash = cashStockService.selectCash(page, limit);
        //查行数selectCashCount
        int count = cashStockService.selectCashCount();
        System.out.println("行数"+count);
        //map集合
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",cash);
        return map;
    }
}

